﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;
using System.Data.Common;

namespace QLHPHP
{
    /// <summary>
    /// Class dai dien cho danh sach cac doi tuong
    /// </summary>
    class DoiTuong
    {
        static DoiTuong()
        { 
        
        }
        /// <summary>
        /// Lay danh sach cac doi tuong dac biet
        /// </summary>
        /// <returns></returns>
        public static DataTable LayDoiTuong()
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "GetObjects";
            return DataAccess.ExecuteSelectCommand(comm);
        }

        /// <summary>
        /// Them doi tuong mien giam hoc phi
        /// </summary>
        /// <param name="objectId">Ma doi tuong</param>
        /// <param name="objectName">Ten doi tuong mien giam</param>
        /// <param name="discountRate">Ti le mien giam</param>
        /// <returns></returns>
        public static bool TaoDoiTuong(string objectId, string objectName, float discountRate)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "CreateObject";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@MaDoiTuong";
            param.DbType = DbType.String;
            param.Value = objectId;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@TenDoiTuong";
            param.DbType = DbType.String;
            param.Value = objectName;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@TiLeGiamHocPhi";
            param.DbType = DbType.Double;
            param.Value = discountRate;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Cap nhat thong tin doi tuong mien giam hoc phi
        /// </summary>
        /// <param name="objectId">Ma doi tuong</param>
        /// <param name="objectName">Ten doi tuong mien giam</param>
        /// <param name="discountRate">Ti le mien giam</param>
        /// <returns></returns>
        public static bool CapNhatDoiTuong(string objectId, string objectName, float discountRate)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "UpdateObject";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@MaDoiTuong";
            param.DbType = DbType.String;
            param.Value = objectId;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@TenDoiTuong";
            param.DbType = DbType.String;
            param.Value = objectName;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@TiLeGiamHocPhi";
            param.DbType = DbType.Double;
            param.Value = discountRate;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Xoa doi tuong khoi danh sach cac doi tuong duoc mien giam hoc phi
        /// </summary>
        /// <param name="objectId">Ma doi tuong</param>
        /// <returns></returns>
        public static bool XoaDoiTuong(string objectId)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "DeleteObject";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@MaDoiTuong";
            param.DbType = DbType.String;
            param.Value = objectId;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);  
        }

        public static bool KiemTraTonTai(string id)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "CheckObjectExist";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@MaDoiTuong";
            param.DbType = DbType.String;
            param.Value = id;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@TonTai";
            param.DbType = DbType.Boolean;
            param.Direction = ParameterDirection.Output;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);

            bool check = (bool)comm.Parameters[1].Value;

            return check;  
        }
    }
}
